/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kamus.manager;

import com.kamus.DB;
import com.kamus.model.KosakataBaru;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class KosakataBaruMgr extends BaseMgr{

    private static KosakataBaruMgr instance;

    public KosakataBaruMgr() {
        connection = getConnection();
    }

    public synchronized  static KosakataBaruMgr getInstance() {
        if (instance == null) {
            instance = new KosakataBaruMgr();
        }
        return instance;
    }

    public synchronized  int insertKata(KosakataBaru kata) {
        int id = -1;
        cekConnection();
        try {
            psmnt = connection.prepareStatement("insert into KOSAKATA_BARU(KATA_IND,KATA_GAUL,TANGGAL,AUTHOR) values(?,?,?,?)");
            psmnt.setString(1, kata.getKataInd());
            psmnt.setString(2, kata.getKataGaul());
            psmnt.setDate(3, new java.sql.Date(new Date().getTime()));
            psmnt.setString(4, kata.getAuthor());
            id =psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return id;
    }

    public void deleteKata(int id) {
        cekConnection();
        try {
            psmnt = connection.prepareStatement("delete from KOSAKATA_BARU where ID=?");
            psmnt.setInt(1, id);
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ArrayList<KosakataBaru> getKatas(String order) {
        cekConnection();
        ArrayList<KosakataBaru> list = new ArrayList<KosakataBaru>();
        KosakataBaru kata;
        try {
            psmnt = connection.prepareStatement("select * from KOSAKATA_BARU order by "+order);
            rs = psmnt.executeQuery();
            while (rs.next()) {
                kata = new KosakataBaru();
                kata.setId(rs.getInt("ID"));
                kata.setKataInd(rs.getString("KATA_IND"));
                kata.setKataGaul(rs.getString("KATA_GAUL"));
                kata.setTanggal(rs.getDate("TANGGAL"));
                kata.setAuthor(rs.getString("AUTHOR"));
                list.add(kata);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

    public void updateKata(KosakataBaru kata) {
        cekConnection();
        try {
            psmnt = connection.prepareStatement("update KOSAKATA_BARU set KATA_IND=?, KATA_GAUL=?, TANGGAL=?, AUTHOR=? where ID= ?");
            psmnt.setString(1, kata.getKataInd());
            psmnt.setString(2, kata.getKataGaul());
            psmnt.setDate(3, new java.sql.Date(kata.getTanggal().getTime()));
            psmnt.setString(4, kata.getAuthor());
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
